package org.example.myleet.rosalind.seto;

import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) throws IOException {
        Solution solution = new Solution();
        String path = solution.getClass().getResource("").getPath() + "example.txt";
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        int n = Integer.parseInt(br.readLine());
        String aSetStr = br.readLine();
        String bSetStr = br.readLine();
        br.close();
        Set<Integer> u = new HashSet<>(n);
        for (int i = 1; i <= n; ++i) {
            u.add(i);
        }
        Set<Integer> a = parse(aSetStr);
        Set<Integer> b = parse(bSetStr);
        printSet(union(a, b));
        printSet(intersection(a, b));
        printSet(exclude(a, b));
        printSet(exclude(b, a));
        printSet(complement(u, a));
        printSet(complement(u, b));
    }

    public static Set<Integer> union(Set<Integer> a, Set<Integer> b) {
        Set<Integer> res = new HashSet<>(a);
        res.addAll(b);
        return res;
    }

    public static Set<Integer> intersection(Set<Integer> a, Set<Integer> b) {
        Set<Integer> res = new HashSet<>(a);
        res.retainAll(b);
        return res;
    }

    public static Set<Integer> exclude(Set<Integer> a, Set<Integer> b) {
        Set<Integer> res = new HashSet<>(a);
        res.removeAll(b);
        return res;
    }

    public static Set<Integer> complement(Set<Integer> u, Set<Integer> a) {
        return exclude(u, a);
    }

    private static Set<Integer> parse(String setStr) {
        String subStr = setStr.substring(1, setStr.length() - 1);
        String[] numbers = subStr.split(", ");
        Set<Integer> set = new HashSet<>(numbers.length);
        for (String number : numbers) {
            set.add(Integer.parseInt(number));
        }
        return set;
    }

    private static void printSet(Set<Integer> set) {
        List<Integer> list = new ArrayList<>(set);
        list.sort(Comparator.naturalOrder());
        StringBuilder sb = new StringBuilder();
        sb.append('{');
        for (int i : list) {
            sb.append(i).append(',').append(' ');
        }
        if (list.size() > 0) {
            sb.delete(sb.length() - 2, sb.length());
        }
        sb.append('}');
        System.out.println(sb);
    }
}
